package zhuzx.generator;

import org.zhuzx.util.GeneralUtil;

public class ClassSourceCodeUtil {

	/**
	 * 生成简单的类源码，只有属性声明和基本的get、set方法。
	 * @param fieldMetadata 由字段信息组成的二维数组，每个子数组对应一个字段，其中有四个元素：字段名，类型，长度（暂未使用），注释。
	 * @param className 指定的类名
	 */
	public static StringBuilder createSimpleCode(String[][] fieldMetadata, String className) {
		StringBuilder src = new StringBuilder();
		src.append("public class " + className + " {\n");
		for (String[] field : fieldMetadata) {
			//("\t/**\n\t * " + field[2] + "\n\t */\n")
			String comment = GeneralUtil.isBlank(field[2]) ? "" : ("\t/**" + field[2] + "*/\n");
			src.append("\n" + comment);
			src.append("	private " + field[1] + " " + field[0] + ";\n");
		}
		src.append('\n');
		for (String[] field : fieldMetadata) {
			String temp = field[0];
			temp = temp.substring(0, 1).toUpperCase() + temp.substring(1);
			String comment = GeneralUtil.isBlank(field[2]) ? "" : ("\t/**" + field[2] + "*/\n");
			src.append("\n" + comment);
			src.append("	public " + field[1] + " get" + temp + "() {\n");
			src.append("		return " + field[0] + ";\n");
			src.append("	}\n");
			src.append(comment);
			src.append("	public void " + "set" + temp
					+ "(" + field[1] + " " + field[0] + ") {\n");
			src.append("		this." + field[0] + " = " + field[0] + ";\n");
			src.append("	}\n");
		}
		src.append('}');
		return src;
	}
	
}